home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Collections: MegaDisc
/
MegaDisc 42 (1994-11)(MegaDisc Digital Publishing)(AU)(Disk 1 of 2)[WB].zip
/
MegaDisc 42 (1994-11)(MegaDisc Digital Publishing)(AU)(Disk 1 of 2)[WB].adf
/
Programming
/
PASCAL_TUTES
/
Tute6.pas
/
Tute6.pas
Wrap
Pascal/Delphi Source File
|
1994-12-12
|
8KB
|
237 lines
{
«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»
«» «»
«» TUTORIAL SIX «»
«» «»
«»
by
«»
«» «»
«»
Anthony Peck
«»
«» «»
«» «»
«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»
:)
6x9=
42
:)
6x9=
42
:)
6x9=
42
:)
6x9=
42
:)
6x9=
42
:)
It's the beginning of the end...
The "if" statement that we saw showcased in tute3.pas was
marvellous, but it was a bit limited. What if we wanted to execute
more than one command for each switch? Can it be done? You bet
your left index finger it can!
When we want a series of commands to be executed, we need to set up
a little "sub-routine" that just contains those commands. We mark
this sub-routine with new "begin..end" commands. The code
that lives between the "begin..end" commands is called a
programming block
. It's like an ice block only warmer...
Enough theory, let's have a bash at a program. This program will
take a number supplied by the user, and calculate the square root.
The square root is a number that, when multiplied by itself, gives
you your original number. For example, 3 is the square root of 9
(3x3=9). Pascal provides an inbuilt function called sqrt for this
purpose, so we'll use it!
}
Program MDTute6 (input,output);
{ This program will calculate the square root
of a number supplied by the user
Author : A N Peck
Date : 25 August 1994 }
const
tab = ' ';
var
num: single;
{
^
|
|__
See tute5.pas for the answer to this and many other
unspeakable questions...
}
begin
writeln;
write(tab,'Please enter in a positive num: ');
readln(num);
writeln;
if num >= 0 then
{
^
|
|__
This sign means "greater than or equal to", and so the
"if" statement is checking to see that the user has
supplied a positive number.
There is another way of writing this...
if not num < 0 then
It looks horrible but it does work! Translated it
says, "If the number is not less than zero". In
other words, if the number is positive.
}
begin
{
^
|
|__
Here is our second "begin" statement. This
one doesn't indicate the start of another
program but rather the start of our first
"subroutine", or programming block.
"If" the number supplied (num) is positive "then"
ALL of the commands which follow this "begin"
command will be executed. The compiler will stop
when it sees the "end".
Notice that I've indented this "begin" so that
the code is a bit easier to read. If you indent
in this way, it can be helpful when you're
troubleshooting (debugging).
}
writeln(tab,'The square root is',sqrt(num):0:4);
{
^
|
|__
This is an inbuilt
function which finds
the square root
of a number
This begin..end block has only one command,
and so we don't strictly need it, but I've left
it in there to illustrate the point. See the
notes at the end of the code for a more expanded
explanation!
}
end
{
^
|
|__
Note no semi-colon at the end of this "end"!
That's because the line isn't finished yet...
}
else
begin
{
^
|
|__
If the number supplied (num) is negative then this
next series of commands are executed!
}
writeln(tab,'That''s a negative number!');
writeln;
writeln(tab,'I can''t give you the square root of ',
num:0:2);
{
^
|
|__
I've just split this line up
to make it look pretty (ahem)...
}
end;
{
^
|
|__
The end of the begin..end block which in this case
contained three "sub"-commands. Is a sub-command like
DIVE, or UP PERISCOPE, or BLOW THE TANKS!!! }
writeln;
end.
{
So what happened? See diagram_three for a flowchart. It often
helps to "see" the logic rather than using your imagination. A
picture is worth a thousand words and all that!
These begin..end blocks can be used as many times as you wish
in the program, and the tricky thing for a long program is matching
all beginnings with all the ends.
An example might be...
begin
begin
begin
begin
writeln('Hi there nested statement!');
end;
end;
end;
end.
It's kinky, but it works as long as for each "begin" there's a
matching "end".
It's possible to use "begin..end" blocks with the "case"
command. In fact, you can use it anywhere, as long as you
have the right government permit (S/NL 34-a22).
In tute4.pas we looked at switching an age variable (called
"yourage") using a "case" command. If you're really bored you
could go back and have a crack at rewriting that code to include
more than one comment for each age bracket. I'll give you the
basic format...
case variable of
1st range: begin
.
.
commands
.
.
end;
2nd range: begin
.
.
commands
.
.
end;
etc...
end;
Try it before you go to bed tonight, and don't forget to brush
your teeth (good dental hygiene is essential in these uncertain
times).
«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»
«» «»
«»
Finis!
«»
«» «»
«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»
:)
6x9=
42
:)
6x9=
42
:)
6x9=
42
:)
6x9=
42
:)
6x9=
42
:)
}